package com.webfilter.demo.filter;

import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 拦截需要全路径比如/push 或者/val/push，
@WebFilter(urlPatterns={"/push"},filterName = "MyFilter") //针对什么连接做处理
//@Configuration 如果再加上@Configuration 或者 @Component注解。过滤路径不起作用，会对所有的 sevlet 进行过滤 等价于urlPatterns={"/*"}
public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();
//        requestURI = requestURI.substring(0,requestURI.indexOf("/push"));

        //做转发
        request.getRequestDispatcher(requestURI).forward(request,response);
        return;

    }


}
